package com.bwjf.authority.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.bwjf.authority.biz.AuthorityUserBiz;
import com.bwjf.authority.commons.base.action.BaseAction;
import com.bwjf.authority.commons.base.entity.ResultData;
import com.bwjf.authority.commons.base.entity.ResultMsg;
import com.bwjf.authority.entity.AuthorityUser;
import com.bwjf.authority.entity.validate.UserLoginValidate;

/**
 * <p>
 * 菜单表 前端控制器
 * </p>
 *
 * @author LiLuQing
 * @since 2017-03-03
 */
@Controller
public class AuthorityIndexAction extends BaseAction {
    
    @Autowired
    AuthorityUserBiz authorityUserBiz;
    
    @RequestMapping("/")
    public String entrance() {
        return "redirect:/login";
    }
    
    @RequestMapping(path="/login", method=RequestMethod.GET)
    public String tologin(ModelMap modelMap) {
        return "/login.html";
    }
    
    /**
     * 登陆功能
     * @return
     */
    @RequestMapping(path="/login", method=RequestMethod.POST, produces="application/json;charset=utf-8")
    @ResponseBody
    public String login(@Validated(UserLoginValidate.class) AuthorityUser user, BindingResult validatedResult
            , HttpSession session) {
        ResultMsg resultMsg=new ResultMsg(validatedResult);
        if(resultMsg.getState()){
            ResultData<AuthorityUser> result=authorityUserBiz.login(user);
            if(result.getState()){
                session.setAttribute("user", result.getData());
            }
            return result.toJSONString();
        }
        return resultMsg.toJSONString();
    }
    
    @RequestMapping(path="/index", method=RequestMethod.GET)
    public String toindex(ModelMap modelMap) {
        return "/index.html";
    }
    
    @RequestMapping(path="/main", method=RequestMethod.GET)
    public String toMain(ModelMap modelMap) {
        return "/main.html";
    }
}
